<template>
  <div id="app-container" style="margin: 20px">
    <!--条件查询表单-->
    <el-form :inline="true" >
      <el-form-item label="靶场名称">
        <el-input v-model="queryTrainingDTO.trainingName" size="small" style="width: 300px" placeholder="请输入靶场名称"/>
      </el-form-item>
      <!--      <el-form-item label="靶场父ID">-->
      <!--        <el-input v-model="queryTrainingDTO.training_parentId" size="small" placeholder="请输入靶场父ID"/>-->
      <!--      </el-form-item>-->
      <el-form-item label="靶场难度" >
        <el-select v-model="queryTrainingDTO.difficulty" size="small" clearable placeholder="请选择靶场难度">
          <el-option :value="0" label="低"/>
          <el-option :value="1" label="中"/>
          <el-option :value="2" label="高"/>
        </el-select>
      </el-form-item>
      <el-form-item label="靶场价格">
        <el-input v-model="queryTrainingDTO.price" size="small" placeholder="靶场价格"/>
      </el-form-item>

    </el-form>
    <el-form :inline="true" >

      <el-form-item label="是否vip">
        <el-select v-model="queryTrainingDTO.isVip" size="small" clearable placeholder="是否vip">
          <el-option :value="0" label="非VIP靶场"/>
          <el-option :value="1" label="VIP靶场"/>
        </el-select>
      </el-form-item>

      <el-form-item label="创建时间">
        <el-date-picker
          v-model="queryTrainingDTO.createTimeStart"
          size="small"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"/>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="queryTrainingDTO.createTimeEnd"
          size="small"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"/>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="pageListQuery()">查询</el-button>
        <el-button plain icon="el-icon-refresh" size="small" @click="resetData()">清空</el-button>

      </el-form-item>
    </el-form>

    <el-form :inline="true">
      <router-link to="/training/add">
        <el-button type="primary" plain icon="el-icon-plus" size="small">新增</el-button>
      </router-link>
      <el-button type="warning" plain icon="el-icon-delete" size="small" @click="deleteBySelect">批量删除</el-button>
      <el-button type="danger" plain icon="el-icon-download" size="small" @click="handleExport">导出</el-button>
    </el-form>

    <!--数据显示列表-->
    <el-table
      :data="trainingList"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="45"
        align="center"/>
      <el-table-column label="序号" width="50" align="center">
        <template slot-scope="scope">
          {{ (currentPage-1)*pageSize + scope.$index +1 }}
        </template>
      </el-table-column>
      <!--      <el-table-column prop="id" label="靶场ID" width="100" align="center"/>-->
      <el-table-column prop="trainingName" label="靶场标题" width="280" align="center"/>

      <el-table-column prop="avatar" label="靶场图片" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" width="60px" height="60px">
        </template>
      </el-table-column>

      <!--      <el-table-column prop="difficulty" label="靶场难度" width="100" align="center"/>-->
      <el-table-column prop="difficulty" label="靶场难度" width="100" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.difficulty=='0'">低</span>
          <span v-if="scope.row.difficulty=='1'">中</span>
          <span v-if="scope.row.difficulty=='2'">高</span>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="靶场价格" width="100" align="center"/>
      <!--      <el-table-column prop="isVip" label="是否VIP" width="100" align="center"/>-->
      <el-table-column label="Vip状态" width="100" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isVip"
            :active-value="1"
            :inactive-value="0"
            active-color="#FFD700"
            @change="changeSwitchVIP(scope.row)"/>

        </template>
      </el-table-column>

      <el-table-column prop="createTime" label="创建时间" width="160" align="center"/>
      <el-table-column prop="updateTime" label="更新时间" width="160" align="center"/>

      <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <router-link :to="'/training/update/'+scope.row.id">
            <el-button
              type="text"
              icon="el-icon-edit"
              size="mini">编辑</el-button>
          </router-link>
          <el-button
            type="text"
            icon="el-icon-delete"
            size="mini"
            @click="handleDelete(scope.row.id)">删除</el-button>
          <router-link :to="'/experiment/update/'+scope.row.id">
            <el-button
              type="text"
              icon="el-icon-edit"
              size="mini"
            >详情
            </el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
import training from '../../api/training'
import fileDownload from 'js-file-download'
import axios from 'axios'

export default {
  data() {
    return {
      queryTrainingDTO: {},
      trainingList: [],
      currentPage: 1,
      pageSize: 10,
      total: 1,
      ids: [],
      multipleSelection: ['']
    }
  },
  created() {
    this.initDate()
  },
  methods: {
    pageListQuery() {
      this.initDate()
    },
    resetData() {
      this.queryTrainingDTO = {}
      this.initDate()
    },
    changeSwitchVIP(row) {
      this.$confirm('是否变更VIP状态', '提示信息', { type: 'warning' }).then(() => {
        console.log(row.isVip)
        training.changeSwitchVIP(row.id, row.isVip).then(response => {
          this.$message.success('状态变更成功')
        })
      }).catch(() => {
        this.$message.info('取消变更操作')
        row.isVip = (row.isVip > 0) ? 0 : 1
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.initDate()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
      this.initDate()
    },
    handleDelete(id) {
      this.$confirm('是否确定删除', '提示信息', { type: 'warning' }).then(() => {
        this.ids.push(id)
        training.deleteOneTraining(this.ids).then(response => {
          this.$message.success('删除成功')
          this.initDate()
          this.ids = []
        })
      }).catch(() => {
        this.$message.info('取消删除操作')
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = []
      for (let i = 0; i < val.length; i++) {
        if (this.multipleSelection.indexOf(val[i].id) === -1) {
          this.multipleSelection.push(val[i].id)
        }
      }
      console.log(this.multipleSelection)
    },
    deleteBySelect() {
      this.$confirm('是否确定删除', '提示信息', { type: 'warning' }).then(() => {
        training.deleteSelectTraining(this.multipleSelection).then(response => {
          this.$message.success('删除成功')
          this.multipleSelection = []
          this.initDate()
        })
      }).catch(() => {
        this.$message.info('取消删除操作')
      })
    },
    handleExport() {
      axios({
        method: 'post',
        url: 'http://localhost:8080/training/export',
        responseType: 'blob',
        data: this.queryTrainingDTO
      }).then(res => {
        if (res && res.data) {
          fileDownload(res.data, '靶场信息.xlsx')
        }
      })
    },
    initDate() {
      training.listPage(this.currentPage, this.pageSize, this.queryTrainingDTO).then(response => {
        this.trainingList = response.data.data
        this.total = response.data.total
      })
    }
  }
}
</script>
